/*
 * SectionLists.java
 *
 * Created on September 11, 2009, 4:30 PM
 */

package Section;
import Section.*;
import Department.*;
import DataConnect.DBHelper;
import java.sql.Connection;
import tool.Check;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author  MyLaptop
 */
public class SectionLists extends javax.swing.JPanel {
    private DefaultTableModel modelSection = new DefaultTableModel();
    private DefaultComboBoxModel cbmdDepartment = new DefaultComboBoxModel();
    Department objDept = new Department();
    
    String nameTable = "Section";
    
    
    
    Vector vLoad = new Vector();
    /** Creates new form SectionLists */
    public SectionLists() {
        initComponents();
         try {
            txtCode.setEnabled(false);
            vLoad = Section.getAllSection();
            this.loadTableSection(nameTable,vLoad);
            

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
         

         
    }
     public void loadTableSection(String nameTable,Vector vLoad) throws SQLException{
        modelSection.setNumRows(0);
        Vector columnSection = new Vector();
        
        columnSection.addElement("ID");
        columnSection.addElement("SectionName");
        columnSection.addElement("DepartmentID");
                
        modelSection.setColumnIdentifiers(columnSection);        
        Section obj = new Section();
        for(int i=0; i< vLoad.size();i++){
            obj = (Section) vLoad.get(i);
            Vector rowSection = new Vector();
            rowSection.addElement(obj.getSectionID());
            rowSection.addElement(obj.getSectionName());
            rowSection.addElement(obj.getDepartmentID());
            modelSection.addRow(rowSection);
        }
        tableSection.setModel(modelSection);
        
        Vector vDept=new Vector();
        vDept=Department.getAllDepartment();
        cbmdDepartment.removeAllElements();
        cbmdDepartment.addElement("---- Choice Department -----");          
            for(int j=0;j<vDept.size();j++) {
                objDept=(Department)vDept.get(j);
                cbmdDepartment.addElement(objDept.getDepartmentName());
            }
        cbDept.setModel(cbmdDepartment);
        
        
        
    }
    public void checkSectionName(){       
        String name = txtName.getText().trim();       
        boolean test = false;
        try {
            ResultSet rs = DBHelper.executeQuery("spGetSectionName");
            while(rs.next()){
                if(name.equals(rs.getString("sectionName"))){
                    txtName.setText("");
                    txtName.requestFocus();
                    test = true;                      
                }
            }
            if(test == true){
                JOptionPane.showMessageDialog(this,"Section Name existed !","Add new Section",JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    public boolean checkSection(){        
        Check c= new Check();
        if(!c.checkSpace(txtName.getText()) || !c.check(txtName.getText())){
            JOptionPane.showMessageDialog(this,"Section Name Invalid","Add new Section",JOptionPane.ERROR_MESSAGE);
            txtName.setText("");
            txtName.requestFocus();
            return false;
        }
      
        return true;
    }
    
    public void add(){
        int test = 0;
        if(!checkSection()){
            return;
        }         
        Section obj = new Section();
        obj.setSectionName(txtName.getText().trim());
       // obj.setDepartmentID(test);
        try{
             Vector vDept=new Vector();
        vDept=Department.getAllDepartment();
        for(int j=0;j<vDept.size();j++) {
            objDept=(Department)vDept.get(j);
            if(cbDept.getSelectedItem().equals(objDept.getDepartmentName())){
                obj.setDepartmentID(objDept.getDepartmentID());                    
            }           
         }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        
        
       
        try {
            test = Section.insertSection(obj);
            if(test ==1){
                JOptionPane.showMessageDialog(this,"Add new Section successfully !","Add new Section",JOptionPane.INFORMATION_MESSAGE);
                vLoad = Section.getAllSection();
                this.loadTableSection(nameTable,vLoad);
            }else
                JOptionPane.showMessageDialog(this,"Add new Section error","Add new Section",JOptionPane.ERROR_MESSAGE);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
    }
    
    public void update(){
        int test = 0;
        if(!this.checkSection()){
            return;
        }
        Section obj = new Section();
        int sectionID = Integer.parseInt(txtCode.getText().trim());
        obj.setSectionID(sectionID);
        obj.setSectionName(txtName.getText().trim());
        try{
             Vector vDept=new Vector();
        vDept=Department.getAllDepartment();
        for(int j=0;j<vDept.size();j++) {
            objDept=(Department)vDept.get(j);
            if(cbDept.getSelectedItem().equals(objDept.getDepartmentName())){
                obj.setDepartmentID(objDept.getDepartmentID());                    
            }           
         }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        
        
        try {            
            test = Section.updateSection(obj);
            if(test == 1){
                JOptionPane.showMessageDialog(this,"Update Section successfully !","Update Section",JOptionPane.INFORMATION_MESSAGE);
                vLoad = Section.getAllSection();
                this.loadTableSection(nameTable,vLoad);
            }else
                JOptionPane.showMessageDialog(this,"Update Section error","Update Section",JOptionPane.ERROR_MESSAGE);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public void delete(){
        int show = JOptionPane.showConfirmDialog(this,"Are you sure delete Section ?","Remove Section",JOptionPane.YES_NO_OPTION);
        if(show == JOptionPane.YES_OPTION){
            int test = 0, count = 0;
            int [] rArr = tableSection.getSelectedRows();
            try{
                for(int i = 0; i < rArr.length; i++){
                    test = Section.deleteSection(tableSection.getValueAt(rArr[i],1).toString());
                    if(test == 1)
                        count++;
                }
                if(count > 0){
                    JOptionPane.showMessageDialog(this,"Delete "+count+" Section","Remove Section",JOptionPane.INFORMATION_MESSAGE);
                    vLoad = Section.getAllSection();
                    this.loadTableSection(nameTable,vLoad);
                }else
                    JOptionPane.showMessageDialog(this,"Delete Section error !","Remove Section",JOptionPane.ERROR_MESSAGE);
            }catch(SQLException ex){
                System.err.println(ex.getMessage());
            }
        }
    }
    
    public void resetForm(){
        txtName.setText("");
           
        
        txtName.requestFocus();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableSection = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cbDept = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel3 = new javax.swing.JPanel();
        btnReset = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Section List"));

        tableSection.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableSection.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableSectionMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableSection);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 611, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Section Information Details"));

        jLabel1.setText("ID");

        jLabel2.setText("Name");

        jLabel3.setText("Department");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(39, 39, 39)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                    .addComponent(cbDept, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbDept, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/reset.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Update.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(btnAdd)
                .addGap(32, 32, 32)
                .addComponent(btnUpdate)
                .addGap(32, 32, 32)
                .addComponent(btnDelete)
                .addGap(29, 29, 29)
                .addComponent(btnReset)
                .addContainerGap(32, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnAdd)
                .addComponent(btnDelete)
                .addComponent(btnReset))
        );

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/department.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 826, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(119, 119, 119)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
// TODO add your handling code here:
        this.resetForm();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
// TODO add your handling code here:
        this.delete();
        this.resetForm();
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
// TODO add your handling code here:
        this.update();
        this.resetForm();
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
        this.checkSectionName();
        this.add();
    }//GEN-LAST:event_btnAddActionPerformed

    private void tableSectionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableSectionMouseClicked
// TODO add your handling code here:
        int r = tableSection.getSelectedRow();
        if(r < 0){
            return;
        }
        txtCode.setText(""+tableSection.getValueAt(r,0));
        txtName.setText(""+tableSection.getValueAt(r,1));
        
        try{
             Vector vDept=new Vector();
            vDept=Department.getAllDepartment();
            for(int j=0;j<vDept.size();j++) {
                objDept=(Department)vDept.get(j);
                if(tableSection.getValueAt(r,2).equals(objDept.getDepartmentID())){
                    cbDept.setSelectedItem(objDept.getDepartmentName());
                }
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        
        
    }//GEN-LAST:event_tableSectionMouseClicked
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cbDept;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tableSection;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
    
}
